package com.chanzany.principle.ocp;

public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}
class Shape{
    int m_type;
}
class Rectangle extends Shape{
    Rectangle(){super.m_type=1;}
}
class Circle extends Shape{
    Circle(){super.m_type=2;}
}
//新增画三角形
class Triangle extends Shape{
    Triangle(){super.m_type=3;}
}
//这是一个用于绘图的类
class GraphicEditor{
    //接收Shape对象，然后根据type，来绘制不同的图形
    public void drawShape(Shape s){
        if (s.m_type==1){ drawRectangle(s); }
        else if (s.m_type==2){ drawCircle(s); }
        //需要修改原有的功能代码
        else if (s.m_type==3){ drawTriangle(s); }
    }
    //需要新增的方法
    private void drawTriangle(Shape t) {
        System.out.println("绘制三角形");
    }
    private void drawCircle(Shape c) {
        System.out.println("绘制圆形");
    }
    private void drawRectangle(Shape r) {
        System.out.println("绘制矩形");
    }
}
